package com.easychat.service.impl;

import com.easychat.common.LeaseException;
import com.easychat.common.PageResult;
import com.easychat.entity.UserInfo;
import com.easychat.entity.UserInfoBeauty;
import com.easychat.entity.query.UserInfoBeautyQuery;
import com.easychat.enums.BeautyAccountStatusEnum;
import com.easychat.mappers.UserInfoBeautyDao;
import com.easychat.mappers.UserInfoDao;
import com.easychat.service.UserInfoBeautyService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 靓号表(UserInfoBeauty)表服务实现类
 *
 * @author makejava
 * @since 2024-10-01 17:32:20
 */
@Service("userInfoBeautyService")
public class UserInfoBeautyServiceImpl implements UserInfoBeautyService {
    @Resource
    private UserInfoBeautyDao userInfoBeautyDao;
    @Resource
    private UserInfoDao userInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserInfoBeauty queryById(Integer id) {
        return this.userInfoBeautyDao.queryById(id);
    }

    @Override
    public PageResult queryByPage(UserInfoBeautyQuery userInfoBeautyQuery) {
        UserInfoBeauty userInfoBeauty = new UserInfoBeauty();
        BeanUtils.copyProperties(userInfoBeautyQuery,userInfoBeauty);
        String orderBy = userInfoBeautyQuery.getOrderBy();
        PageHelper.startPage(userInfoBeautyQuery.getPageNumber(),userInfoBeautyQuery.getPageSize());
        PageHelper.orderBy(orderBy + " desc");
        List<UserInfoBeauty> userInfoBeauties = userInfoBeautyDao.queryAllByLimit(userInfoBeauty);
        long count = userInfoBeautyDao.count(userInfoBeauty);
        PageResult pageResult = new PageResult(count,userInfoBeauties);
        return pageResult;
    }


    /**
     * 新增数据
     *
     * @param userInfoBeauty 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(UserInfoBeauty userInfoBeauty) {
        int insert = this.userInfoBeautyDao.insert(userInfoBeauty);
        boolean result = insert > 0 ? true : false;
        return result;
    }

    /**
     * 修改数据
     *
     * @param userInfoBeauty 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(UserInfoBeauty userInfoBeauty) {
        int update = userInfoBeautyDao.update(userInfoBeauty);
        if(update > 0){
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userInfoBeautyDao.deleteById(id) > 0;
    }

    /**
     * 添加靓号
     * @param beauty
     */
    @Override
    public void saveAccount(UserInfoBeauty beauty) {
        if(beauty.getId()!= null){
            UserInfoBeauty dbInfo = userInfoBeautyDao.selectById(beauty.getId());
            if(BeautyAccountStatusEnum.USEED.getStatus().equals(dbInfo.getStatus())){
                throw new LeaseException("该靓号已被使用");
            }
        }
        UserInfoBeauty dbInfo = userInfoBeautyDao.selectByEmail(beauty.getEmail());
        //新增的时候判断邮箱是否存在
        if(beauty.getId() == null && dbInfo != null){
            throw new LeaseException("该靓号邮箱已存在");
        }
        //修改的时候判断邮箱是否存在
        if(beauty.getId() != null && dbInfo != null && dbInfo.getId() != null && !beauty.getId().equals(dbInfo.getId())){
            throw new LeaseException("该靓号邮箱已存在");
        }
        //判断靓号是否存在
        dbInfo = userInfoBeautyDao.selectByUserId(beauty.getUserId());
        if(beauty.getId() == null && dbInfo != null){
            throw new LeaseException("靓号已经存在");
        }
        if(beauty.getId() != null && dbInfo != null && dbInfo.getId() != null &&!beauty.getId().equals(dbInfo.getId())){
            throw new LeaseException("靓号已经存在");
        }
        //判断邮箱是否已经注册
        UserInfo userInfo = userInfoDao.queryByEmail(beauty.getEmail());
        if(userInfo != null){
            throw new LeaseException("该邮箱已经注册");
        }
        userInfo = userInfoDao.queryById(beauty.getUserId());
        if(userInfo != null){
            throw new LeaseException("该用户已经存在");
        }
        if(beauty.getId() != null){
            userInfoBeautyDao.updateById(beauty,beauty.getId());
        }else {
            beauty.setStatus(BeautyAccountStatusEnum.NO_USE.getStatus());
            userInfoBeautyDao.insert(beauty);
        }


    }
}
